package com.zmzncs.shop.module.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.shop.common.pojo.LoginUser;
import com.zmzncs.shop.common.pojo.PageInfo;
import com.zmzncs.shop.common.pojo.Rest;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.common.util.JwtUtil;
import com.zmzncs.shop.common.util.RestUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shoporder.*;
import org.springframework.web.bind.annotation.*;
import com.zmzncs.shop.module.shop.service.ShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Api(tags = {"商户-订单"})
@RestController
@RequestMapping("/shop/shop-order")
public class ShopOrderController {

    @Autowired
    private ShopOrderService shopOrderService;


    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody @Valid ShopOrderAddForm form){
        shopOrderService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@RequestParam Long id){
        shopOrderService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody @Valid ShopOrderUpdateForm form){
        shopOrderService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Rest<PageInfo<ShopOrderVO>> page(@Valid ShopOrderSearchForm form){
        IPage<ShopOrderDTO> iPage = shopOrderService.page(form);
        PageInfo<ShopOrderVO> pageInfo = JsonUtil.pageTransform(iPage, ShopOrderVO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "详情")
    @GetMapping("get-by-id")
    public Rest<ShopOrderVO> getById(@RequestParam Long id){
        ShopOrderDTO shopOrderDTO = shopOrderService.getById(id);

        ShopOrderVO shopOrderVO = new ShopOrderVO();
        BeanUtils.copyProperties(shopOrderDTO, shopOrderVO);

        return RestUtil.success(shopOrderVO);
    }

    @ApiOperation(value = "根据核销码查询商户订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "writeOffCode", value = "核销码", required = true),
            @ApiImplicitParam(paramType = "query",name = "shopId", value = "商户Id", required = true),
    })
    @GetMapping("get-by-write-off-code")
    public Rest<ShopOrderVO> getByWriteOffCode(@RequestParam String writeOffCode, @RequestParam Long shopId){
        ShopOrderDTO shopOrderDTO = shopOrderService.getByWriteOffCode(writeOffCode,shopId);

        ShopOrderVO shopOrderVO = new ShopOrderVO();
        BeanUtils.copyProperties(shopOrderDTO, shopOrderVO);

        return RestUtil.success(shopOrderVO);
    }

    @ApiOperation(value = "核销商品订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "id", value = "订单Id", required = true),
    })
    @GetMapping("write-off-code")
    public Rest writeOffCode(@RequestParam Long id){

        shopOrderService.writeOffCode(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "首页-今日收入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "shopId", value = "商户Id", required = true),
    })
    @GetMapping("today-revenue")
    public Rest<String> todayRevenue(@RequestParam Long shopId){

        String revenue = shopOrderService.todayRevenue(shopId);
        if (revenue == null ){
            revenue = "0";
        }
        return RestUtil.success(revenue);
    }

    @ApiOperation(value = "首页-收入详情-商品核销收入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "shopId", value = "商户Id", required = true),
            @ApiImplicitParam(paramType = "query",name = "dateType", value = "0昨日，1今日，2本周，3本月", required = true),
    })
    @GetMapping("write-off-revenue")
    public Rest<String> writeOffRevenue(@RequestParam Long shopId, @RequestParam Integer dateType){
        String revenue = shopOrderService.writeOffRevenue(shopId,dateType);
        if (revenue == null){
            revenue = "0";
        }
        return RestUtil.success(revenue);
    }

    @ApiOperation(value = "首页-收入详情-引流投入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "shopId", value = "商户Id", required = true),
            @ApiImplicitParam(paramType = "query",name = "dateType", value = "0昨日，1今日，2本周，3本月", required = true),
    })
    @GetMapping("invest")
    public Rest<String> invest(@RequestParam Long shopId, @RequestParam Integer dateType){
        String revenue = shopOrderService.invest(shopId,dateType);
        if (revenue == null){
            revenue = "0";
        }
        return RestUtil.success(revenue);
    }

    @ApiOperation(value = "首页-收入详情-总收益环比")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "shopId", value = "商户Id", required = true),
    })
    @GetMapping("get-month-income")
    public Rest<List<Double>> getMonthIncome(@RequestParam Long shopId){
        List<Double> incomeList = shopOrderService.getMonthIncome(shopId);
        return RestUtil.success(incomeList);
    }

    @ApiOperation(value = "首页-核销记录")
    @GetMapping("write-off-page")
    public Rest<PageInfo<ShopOrderWriteOffVO>> getWriteOffPage(@Valid ShopOrderWriteOffForm form){
        IPage<ShopOrderDTO> iPage = shopOrderService.getWriteOffPage(form);
        PageInfo<ShopOrderWriteOffVO> pageInfo = JsonUtil.pageTransform(iPage, ShopOrderWriteOffVO.class);
        return RestUtil.success(pageInfo);
    }
}
